'use strict';

angular.module('app')
    .controller('updateController', ['$scope', '$rootScope', '$state', '$timeout',
        function ($scope, $rootScope, $state, $timeout) {
            var title = "";
            
            UE.delEditor("messageContent");
            var ue = UE.getEditor('messageContent',{
                serverUrl:"/iFengChao-SYS-Web/upload/uploadUMImageHui"
            });

            // 提交数据
            $scope.submit = function(){
                $scope.loading = true;

                if(!$('#messageTitle').val()){
                    $scope.alertModal({'content':'请输入资讯标题'});
                    return;
                }
                
                if(!$('#platNo').val()){
                    $scope.alertModal({'content':'请选择资讯平台'});
                    return;
                }
                
                if(!$('#author').val()){
                    $scope.alertModal({'content':'请输入资讯作者'});
                    return;
                }
                
                if(!$('#mediaSource').val()){
                    $scope.alertModal({'content':'请输入媒体来源'});
                    return;
                }
                
                if(!$('#publishSub').val()){
                    $scope.alertModal({'content':'请输入发布栏目'});
                    return;
                }
                
                if (2 == $scope.record.requestFlag) {
                	console.log($('#messageContent1').val());
                	if (!$('#messageContent1').val()) {
                		$scope.alertModal({'content':'请输入资讯内容'});
                        return;
                	}
                	$scope.record.messageContent= $('#messageContent1').val();
                } else {
                	if(!UE.getEditor('messageContent').getContent()){
                        $scope.alertModal({'content':'请输入资讯内容'});
                        return;
                    }
                	
                	$scope.record.messageContent= UE.getEditor('messageContent').getContent();
                }
                
                var detailUrlArr = $scope.detailImgData[0];
                if (!detailUrlArr) {
                	$scope.alertModal({'content':'请上传封面图片'});
                    return;
                }
                
                var picUrl = $scope.detailImgData[0].src;
                $scope.record.messageImg = picUrl[0];
                //$scope.record.messageImg = '';

                saveData();
            };

            function saveData(){
                var m = $scope.record;
                console.log(m);
                if(m){
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
                        url : $scope.record.messageId ? $scope.app.project_name + '/messagePlat/update' : $scope.app.project_name + '/messagePlat/add',
                        data: m
                    }).then(callback);
                }
                function callback(result){
                    if(result.httpCode ==200){
                        $scope.alertModal({"content":"操作成功"});
                        $timeout(function(){
                            $state.go('main.plat.messagePlat.list');
                        },2000);
                    }else{
                        $scope.alertModal({"content":"操作失败"});
                        $scope.isDisabled = false;
                    }
                    $scope.loading = false;
                }
            }
            
            function getMessagePlatList() {
                $scope.loading = true;
                $scope.selected = '';
                $.ajax({
                    url : $scope.app.project_name + '/messagePlat/read/getMessagePlatList'
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.messagePlatList = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
    		}
            
            // 初始化页面
            $scope.activate=function (id) {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name + '/messagePlat/read/detail',
                    data: {'id': id}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                        
                        var messageImg = $scope.record.messageImg;
                    	
                    	$scope.detailImgData = [];
    		            var detailArr = [];
                    	if (messageImg != null && messageImg != "") {
        		            detailArr.push($scope.record.messageImg);
        		            $scope.detailImgData.push({
        	                    src:detailArr
        	                })
                    	}
                    	if (1 == $scope.record.requestFlag) {
                    		UE.getEditor('messageContent').setContent($scope.record.messageContent);
                    	}
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }
            
            if ($state.includes('**.messagePlat.update')) {
                title = "修改资讯";
                var id = $state.params.id;
                $scope.activate(id);
                getMessagePlatList();
                $scope.isDisabled = false;
            } else if ($state.includes('**.messagePlat.add')) {
                title = "添加资讯";
                getMessagePlatList();
                setTimeout(function(){
                    !$rootScope.$$phase && $scope.$apply();
                },300);
            }
            
            $scope.title = $rootScope.title = title;
            $scope.loading = true;
        }]);